package ecmnext.server.gui;

import java.util.Iterator;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import org.jdom.Element;

import ecmnext.business.BusinessCore;
import ecmnext.business.Dividend;
import ecmnext.business.DividendModel;

import ecmnext.business.event.*;
import ecmnext.utils.Utils;
import ecmnext.utils.XmlExportable;
import ecmnext.utils.XmlLoadable;

public class TableModelInterestRate extends AbstractTableModel implements XmlExportable, XmlLoadable {
	
	private Vector<Float> interestRate = new Vector<Float>();
	private boolean cellEditable=true;
	private String[] columnNames = {"Period n?", "Interest Rate (%) "};
	
	public TableModelInterestRate() {
		Utils.logger.error("avant");
		interestRate.setSize(1);
		interestRate.setElementAt(0f,0);
		Utils.logger.error("apres");
	}
	
	public Vector getVectorVal(){
		return interestRate;
	}
	
	public void setVectorVal(Object bidule, int i){
		
	}
	
	public int getColumnCount() {
		return 2;
	}

	public int getRowCount() {
		return interestRate.size();
	}
	
	public Class getColumnClass(int column) {
		return (column==0) ? Integer.class : Float.class;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {

			return new Integer(rowIndex + 1);
		}
		else {
			return (Float)(interestRate.elementAt(rowIndex));//interestRate.elementAt(rowIndex);
		}  

	}
	
	public String getColumnName(int column) {
		return this.columnNames[column];
	}


	public void setValueAt(Object aValue, int rowIndex, int columnIndex){

		if(aValue!=null)
		{
			Utils.logger.error("setValueAt  "+aValue+"  "+rowIndex);
		}

		interestRate.setElementAt((Float)aValue, rowIndex);


	}
	
	public void setInterestSize (int size){
		int oldSize = interestRate.size(); 
		interestRate.setSize(size);
		for (int i = oldSize ; i<size ; i++){
			interestRate.setElementAt(interestRate.elementAt(oldSize-1), i);
		}
		
	}
	public void saveToXml(Element node) {
		node.setAttribute("periodCount", 
				Integer.toString(interestRate.size()));
	    for (int i = 0; i < interestRate.size(); i++) {
	        Element periodRate = new Element("PeriodRate");
	        periodRate.setAttribute("Rate",Float.toString(interestRate.elementAt(i)));
	        node.addContent(periodRate);
	    }
	}
	
	public void fillInterestRate(float interest) {
		for (int i = 0; i < interestRate.size(); i++) {
			interestRate.setElementAt(interest, i);
		}
	}
	
	public void loadFromXml(Element node) {
	    String periodCount = node.getAttributeValue("periodCount");
	    // retro-compatibility
	    if (periodCount == null) 
	    {
	    	Utils.logger.error("invalid xml, assuming it is an older version");
	    	this.fillInterestRate(Float.parseFloat(node.getText()));
	    }
	    else 
	    {
			this.setInterestSize(Integer.parseInt(periodCount));
		    Iterator interestNodes = node.getChildren("PeriodRate").iterator();
		    int i = 0;
		    while(interestNodes.hasNext()) {
		      Element interestNode = (Element)interestNodes.next();
		      this.setValueAt(Float.parseFloat(interestNode.getAttributeValue("Rate")),i,2);
		      i++;
		    }
	    }
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
	    if (cellEditable){
	    	return (columnIndex > 0);
	    }
	    return false;
	}
	
	public void setCellEditable(){
	    this.cellEditable=true;
	}

	public void setCellUneditable(){
		this.cellEditable=false;
	}



	
}
